package com.ruoyi.web.controller.wx;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ByBs;
import com.ruoyi.system.domain.ByCouponUser;
import com.ruoyi.system.domain.ByJz;
import com.ruoyi.system.service.IByBsService;
import com.ruoyi.system.service.IByJzService;
import com.ruoyi.system.service.WxOcrService;
import com.ruoyi.system.service.impl.SaveImgService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/jz")
@Validated
public class WxJzController {

    @Autowired
    IByJzService jzService;

    @Autowired
    private SaveImgService saveImgService;

    @Autowired
    private WxOcrService wxOcrService;

    @PostMapping("payYhkqOrder")
    public AjaxResult payYhkqOrder(@RequestBody String param, HttpServletRequest request){
        JSONObject json = JSONObject.parseObject(param);
        Object result = jzService.payYhkqOrder(json,request);

        return AjaxResult.success(result);
    }


    @PostMapping("getParamByHyk")
    public AjaxResult getParamByHyk(){
        Map<String, Object> map = jzService.getParamByHyk();
        return AjaxResult.success(map);
    }

    @PostMapping("getOcrDetail")
    public AjaxResult getOcrDetail(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        System.out.println(json.toString());
        String url = json.getString("url");
        Object obj = wxOcrService.ocrIdCard(url);

        return AjaxResult.success(obj);
    }

    @PostMapping("saveJzZmSfz")
    public AjaxResult saveJzZmSfz(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
//        System.out.println(json.toString());
        String baseStr = json.getString("base");
        String wxid = json.getString("wxid");
        // 进行保存jpg文件
        if(baseStr.contains("data:image/jpeg;")){
            String accUrl = saveImgService.saveJpg(baseStr);
            Map map = new HashMap();
            map.put("url",accUrl);
            return AjaxResult.success(map);
        }else if(baseStr.contains("data:image/png;")){// 进行保存png文件
            String accUrl = saveImgService.savePng(baseStr);
            Map map = new HashMap();
            map.put("url",accUrl);
            return AjaxResult.success(map);
        }
        return AjaxResult.success();
    }

    @PostMapping("saveJzFmSfz")
    public AjaxResult saveJzFmSfz(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
//        System.out.println(json.toString());
        String baseStr = json.getString("base");
        String wxid = json.getString("wxid");
        // 进行保存jpg文件
        if(baseStr.contains("data:image/jpeg;")){
            String accUrl = saveImgService.saveJpg(baseStr);
            Map map = new HashMap();
            map.put("url",accUrl);
            return AjaxResult.success(map);
        }else if(baseStr.contains("data:image/png;")){// 进行保存png文件
            String accUrl = saveImgService.savePng(baseStr);
            Map map = new HashMap();
            map.put("url",accUrl);
            return AjaxResult.success(map);
        }
        return AjaxResult.success();
    }

    /*@PostMapping("queryCount")
    public Object queryCount(@RequestBody String param){
        // System.out.println(param);
        JSONObject json = JSONObject.parseObject(param);
        String wxid = json.getString("wxid");
        JSONObject result = jzService.queryByWxid(wxid);

        return AjaxResult.success(result);
    }*/

    @PostMapping("bcjz")
    public Object bcjz(@RequestBody ByJz entity){
        // System.out.println(entity.toString());
        JSONObject json = jzService.managSave(entity);
        return AjaxResult.success(json);
    }


    @PostMapping("getDetailBywxid")
    public Object getDetailBywxid(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        JSONObject result = jzService.getDetailBywxid(json);
        return AjaxResult.success(result);
    }


    @PostMapping("upByJz")
    public Object upByBs(@RequestBody ByJz entity){
        jzService.updateByJz(entity);
        return AjaxResult.success();
    }



    /**
     * 根据userid去找优惠券信息
     * @param param userid
     * @return
     */
    @PostMapping("getyhq")
    public Object getyhq(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        List<JSONObject> ByCouponUsers = jzService.getyhq(json.getString("wxid"));
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for(JSONObject user : ByCouponUsers) {
            Map<String, Object> yhq = new HashMap<String, Object>();
            Map<String, String> je = new HashMap<String, String>();
            je.put("price", user.getString("price")+"元");
            yhq.put("je", je);
            Map<String, String> xq = new HashMap<String, String>();
            xq.put("title", "折扣券");
            xq.put("time", "有效期：" + user.getString("stime") + "至" + user.getString("etime"));
            yhq.put("xq", xq);
            result.add(yhq);
        }
        return AjaxResult.success(result);
    }


    /**
     * 获取订单支付时的优惠券（优惠券金额小于订单金额  allprice）
     * @param param userid
     * @return
     */
    @PostMapping("getZfyhq")
    public Object Zfyhq(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        List<JSONObject> ByCouponUsers = jzService.Zfyhq(json.getString("wxid"));
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        for(JSONObject order : ByCouponUsers) {
            Map<String, Object> yhq = new HashMap<String, Object>();
            Map<String, String> je = new HashMap<String, String>();
            je.put("price", order.getString("price")+"元");
            yhq.put("id", order.getString("id"));
            if ("0".equals(order.getString("flag"))){
                yhq.put("flag", true);
            }else {
                yhq.put("flag", false);
            }
            yhq.put("je", je);
            Map<String, String> xq = new HashMap<String, String>();
            xq.put("title", "折扣券");
            xq.put("time", "有效期：" + order.getString("stime") + "至" + order.getString("etime"));
            yhq.put("xq", xq);
            result.add(yhq);
        }
        return AjaxResult.success(result);
    }

}
